package com.hyzx.qbasic.admin.controller.operations;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.domain.service.ILogService;
import com.hyzx.qbasic.admin.domain.service.IOperationsService;
import com.hyzx.qbasic.admin.model.annotation.DbLogIgnore;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.po.BusinessLogPO;
import com.hyzx.qbasic.admin.model.po.ErrorLogPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.DbQueryReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetBusinessLogListReq;
import com.hyzx.qbasic.admin.model.vo.req.admin.GetErrorLogListReq;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.model.vo.res.admin.DbQueryRes;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Collections;
import java.util.Map;

/**
 * Created by 黄江华 on 2017/10/31
 */

@Api(tags = "运维工具", description = "OpsToolController")
@Controller
@RequestMapping("/ops/")
public class OpsToolController extends BaseController {
    @Autowired
    private IOperationsService operationsService;

    @Autowired
    private ILogService logService;

    @SysLog("数据查询工具首页")
    @GetMapping("/dbquery/list")
    public String dbQuery() {
        return "operations/dbquery";
    }


    @SysLog("数据库查询")
    @PostMapping("/dbquery")
    @ResponseBody
    public BaseResModel<DbQueryRes> dbQuery(@Valid @RequestBody DbQueryReq req, BindingResult result) {
        PageInfo<Map<String, Object>> data = operationsService.dbQuery(req);
        if (data == null)
            return BaseResUtils.paramError("数据库查询异常，SQL语法不正确");

        DbQueryRes res = new DbQueryRes();
        res.setData(data);
        if (data.getList().isEmpty())
            res.setCols(Collections.EMPTY_SET);
        else
            res.setCols(data.getList().get(0).keySet());

        return BaseResUtils.success(res);
    }

    @SysLog("数据导出")
    @PostMapping("/dbExport")
    public void dbExport(@RequestParam String sql, HttpServletResponse response) {
        operationsService.dbExport(sql, response);
    }

    @SysLog("业务日志首页")
    @GetMapping("/log/business")
    public String businessLog() {
        return "operations/businessLog";
    }

    @DbLogIgnore
    @SysLog("获取业务日志列表")
    @GetMapping("/log/getBusinessLogList")
    @ResponseBody
    public BaseResModel<PageInfo<BusinessLogPO>> getBusinessLogList(@Valid GetBusinessLogListReq req, BindingResult result) {
        return BaseResUtils.success(logService.getBusinessLogList(req));
    }

    @SysLog("错误日志首页")
    @GetMapping("/log/error")
    public String errorLog() {
        return "operations/errorLog";
    }

    @DbLogIgnore
    @SysLog("获取错误日志列表")
    @GetMapping("/log/getErrorLogList")
    @ResponseBody
    public BaseResModel<PageInfo<ErrorLogPO>> getErrorLogList(@Valid GetErrorLogListReq req, BindingResult result) {
        return BaseResUtils.success(logService.getErrorLogList(req));
    }

    @SysLog("错误日志详情页")
    @GetMapping("/log/errLogDetail")
    public String errLogDetail(int logId, Model model) {
        ErrorLogPO errLogDetail = logService.getErrorLogDetail(logId);
        if (errLogDetail != null) {
            model.addAttribute("errLogDetail", errLogDetail);
        }

        return "operations/errLogDetail";
    }

    @SysLog("应用监控首页")
    @GetMapping("/monitor/info")
    public String appMonitor(HttpServletRequest request, Model model) {
        int port = request.getLocalPort();
        String domain = "http://127.0.0.1:" + port;

        Map<String, Object> monitorInfo = operationsService.getAppMonitorInfo(domain);
        if (monitorInfo != null && !monitorInfo.isEmpty()) {
            model.addAttribute("monitorInfo", monitorInfo);
        }

        return "operations/appMonitor";
    }
}
